<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>模板</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/templates","wgTitle":"cpp/language/templates","wgCurRevisionId":65288,"wgArticleId":8596,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Todo with reason"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/templates","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_templates skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">模板</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="https://zh.cppreference.com/w/cpp/container" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><strong class="selflink">模板</strong><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="template_parameters.html" title="cpp/language/template parameters">形参与实参</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html" title="cpp/language/class template">类模板</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function_template.html" title="cpp/language/function template">函数模板</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="member_template.html" title="cpp/language/member template">类成员模板</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="variable_template.html" title="cpp/language/variable template">变量模板</a><span class="t-mark-rev t-since-cxx14">(C++14)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="template_argument_deduction.html" title="cpp/language/template argument deduction">模板实参推导</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template_argument_deduction.html" title="cpp/language/class template argument deduction">类模板实参推导</a><span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="template_specialization.html" title="cpp/language/template specialization">显式（全）特化</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="partial_specialization.html" title="cpp/language/partial specialization">部分特化</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="dependent_name.html" title="cpp/language/dependent name">待决名</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="parameter_pack.html" title="cpp/language/parameter pack">形参包</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="sizeof....html" title="cpp/language/sizeof..."><span class="t-lines"><span>sizeof...</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="fold.html" title="cpp/language/fold">折叠表达式</a><span class="t-mark-rev t-since-cxx17">(C++17)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="sfinae.html" title="cpp/language/sfinae"> SFINAE</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="constraints.html" title="cpp/language/constraints">制约与概念</a><span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>模板是定义下列之一的 C++ 实体：
</p>
<ul><li> 一族类（<a href="class_template.html" title="cpp/language/class template">类模板</a>），可以是<a href="member_template.html" title="cpp/language/member template">嵌套类</a>
</li><li> 一族函数（<a href="function_template.html" title="cpp/language/function template">函数模板</a>），可以是<a href="member_template.html" title="cpp/language/member template">成员函数</a>
</li><li> 一族类型的别名（<a href="type_alias.html" title="cpp/language/type alias">别名模板</a>）<span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</li><li> 一族变量（<a href="variable_template.html" title="cpp/language/variable template">变量模板</a>）<span class="t-mark-rev t-since-cxx14">(C++14 起)</span>
</li><li> 概念（<a href="constraints.html" title="cpp/language/constraints">制约与概念</a>）<span class="t-mark-rev t-since-cxx20">(C++20 起)</span>
</li></ul>
<p>模板以一或多个<a href="template_parameters.html" title="cpp/language/template parameters">模板形参</a>参数化，形参有三种：类型模板形参、非类型模板形参和模板模板形参。
</p><p>当提供了模板实参，或仅对于<a href="function_template.html#.E6.A8.A1.E6.9D.BF.E5.8F.82.E6.95.B0.E6.8E.A8.E5.AF.BC" title="cpp/language/function template">函数</a><span class="t-rev-inl t-since-cxx17"><span>和<a href="class_template_argument_deduction.html" title="cpp/language/class template argument deduction">类</a></span> <span><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></span></span>模板，当模板实参被推导出时，它们替换各模板形参，以获得模板的一个<i>特化（specialization）</i>，即一个特定类型或一个特定函数左值。特化亦可显式提供：对类和函数模板都允许<a href="template_specialization.html" title="cpp/language/template specialization">全特化</a>，只允许对类模板<a href="partial_specialization.html" title="cpp/language/partial specialization">部分特化</a>。
</p><p>在要求完整对象类型的语境中引用某个类模板特化时，或在要求函数定义存在的语境中引用某个函数模板特化时，除非模板已经被显式特化或显式实例化，否则模板即被<i>实例化（instantiate）</i>（它的代码被实际编译）。类模板的实例化不会实例化其任何成员函数，除非它们也被使用。在连接时，不同翻译单元生成的相同实例被合并。
</p><p>模板的定义必须在隐式实例化点可见，这就是为何模板库通常都在头文件中提供所有模板定义的原因（例如<a rel="nofollow" class="external text" href="http://www.boost.org/doc/libs/release/more/getting_started/unix-variants.html#header-only-libraries">大多数 boost 库仅有头文件</a>）
</p>
<h3><span class="mw-headline" id=".E8.AF.AD.E6.B3.95">语法</span></h3>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>template</b></code> <code><b>&lt;</b></code> <span class="t-spar">形参列表</span> <code><b>&gt;</b></code> <span class="t-spar">requires-子句</span><span class="t-mark-rev t-since-cxx20">(C++20)</span><span class="t-mark">(可选)</span> <span class="t-spar">声明</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>export</b></code> <code><b>template</b></code> <code><b>&lt;</b></code> <span class="t-spar">形参列表</span> <code><b>&gt;</b></code> <span class="t-spar">声明</span>
</td>
<td> (2)
</td>
<td> <span class="t-mark-rev t-until-cxx11">(C++11 前)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>template</b></code> <code><b>&lt;</b></code> <span class="t-spar">形参列表</span> <code><b>&gt;</b></code> <code><b>concept</b></code> <span class="t-spar">概念名</span> <code><b>= </b></code> <span class="t-spar">制约表达式</span> <code><b>;</b></code>
</td>
<td> (3)
</td>
<td> <span class="t-mark-rev t-since-cxx20">(C++20 起)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">声明</span>
</td>
<td> -
</td>
<td>  <a href="class_template.html" title="cpp/language/class template">类（包括 struct 和 union）</a>，<a href="member_template.html" title="cpp/language/member template">成员类或成员枚举类型</a>，<a href="function_template.html" title="cpp/language/function template">函数</a>或<a href="member_template.html" title="cpp/language/member template">成员函数</a>，命名空间作用域的静态数据成员，<span class="t-rev-inl t-since-cxx14"><span><a href="variable_template.html" title="cpp/language/variable template">变量或类作用域的静态数据成员</a>，</span> <span><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></span></span><span class="t-rev-inl t-since-cxx11"><span>或<a href="type_alias.html" title="cpp/language/type alias">别名模板</a></span> <span><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></span></span>的声明。它亦可定义<a href="template_specialization.html" title="cpp/language/template specialization">模板特化</a>。
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">形参列表</span>
</td>
<td> -
</td>
<td>  非空的<a href="template_parameters.html" title="cpp/language/template parameters">模板形参</a>的逗号分隔列表，其中每项是<a href="template_parameters.html#.E9.9D.9E.E7.B1.BB.E5.9E.8B.E6.A8.A1.E6.9D.BF.E5.BD.A2.E5.8F.82" title="cpp/language/template parameters">非类型形参</a>、<a href="template_parameters.html#.E7.B1.BB.E5.9E.8B.E6.A8.A1.E6.9D.BF.E5.BD.A2.E5.8F.82" title="cpp/language/template parameters">类型形参</a>、<a href="template_parameters.html#.E6.A8.A1.E6.9D.BF.E6.A8.A1.E6.9D.BF.E5.BD.A2.E5.8F.82" title="cpp/language/template parameters">模板形参</a>或任何这些的<a href="parameter_pack.html" title="cpp/language/parameter pack">形参包</a>之一。
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">概念名</span><br><span class="t-spar">制约表达式</span>
</td>
<td> -
</td>
<td>  见<a href="constraints.html" title="cpp/language/constraints">制约与概念</a> <span class="t-mark-rev t-since-cxx20">(C++20 起)</span>
</td></tr></table>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx11"><td>
<p><code>export</code> 是一个可选的修饰符，声明模板<i>被导出</i>（用于类模板时，它声明其所有成员也被导出）。对被导出模板进行实例化的文件不需要包含其定义：有声明就足够了。<code>export</code> 的实现稀少而且在细节上互不一致。
</p>
</td>
<td><span class="t-mark-rev t-until-cxx11">(C++11 前)</span></td></tr>
</table>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<p>模板形参列表可以后随一个可选的 <a href="constraints.html#requires_.E5.AD.90.E5.8F.A5" title="cpp/language/constraints"><span class="t-spar">requires-子句</span></a>，它指定各模板实参上的<a href="constraints.html" title="cpp/language/constraints">制约</a>。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<p><br>
</p>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">本节未完成<br>原因：核心语法，模板形参，以及实例化，带出 class_template 和 function_template 间的公共内容 </td></tr></table>
<h2><span class="mw-headline" id=".E6.A8.A1.E6.9D.BF.E6.A0.87.E8.AF.86">模板标识</span></h2>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <span class="t-spar">模板名</span> <code><b>&lt;</b></code> <span class="t-spar">形参列表</span> <code><b>&gt;</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>

 

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">模板名</span>
</td>
<td> -
</td>
<td>  或为指名模板的<a href="name.html" title="cpp/language/identifiers">标识符</a>（该情况下称之为 "<span class="t-spar">简单模板标识</span>" ），或为重载运算符模板或用户定义字面量模板的名字。
</td></tr></table>
<p>指名模板特化的 <span class="t-spar">简单模板标识</span> 指名一个类。
</p><p>指名别名模版特化的 <span class="t-spar">模板标识</span> 指名一个类型。
</p><p>指名函数模板特化的 <span class="t-spar">模板标识</span> 指名一个函数。
</p><p><span class="t-spar">模板标识</span> 仅当符合下列条件才合法
</p>
<ul><li> 实参数量不多于形参，或有形参是模板形参包，
</li><li> 每个无默认模板实参的不可推导的非包形参都有一个实参，
</li><li> 每个模板实参都与对应的模板形参相匹配，
</li><li> 替换每个模板实参到其后续模板形参（若存在）中均成功，而且
</li><li> <span class="t-mark-rev t-since-cxx20">(C++20)</span> 若 <span class="t-spar">模板标识</span> 非待决，则其关联制约得以满足，如下所述。
</li></ul>
<p>无效的 <span class="t-spar">简单模板标识</span> 是编译时错误，除非它指名的是函数模板特化（该情况下可适用 <a href="sfinae.html" title="cpp/language/sfinae">SFINAE</a>）。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">class</span> T, T<span class="sy4">::</span><span class="me2">type</span> n <span class="sy1">=</span> <span class="nu0">0</span><span class="sy1">&gt;</span> <span class="kw1">class</span> X<span class="sy4">;</span>
<span class="kw1">struct</span> S <span class="br0">{</span>
  <span class="kw1">using</span> type <span class="sy1">=</span> <span class="kw4">int</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">using</span> T1 <span class="sy1">=</span> X<span class="sy1">&lt;</span>S, <span class="kw4">int</span>, <span class="kw4">int</span><span class="sy1">&gt;</span><span class="sy4">;</span> <span class="co1">// 错误：过多实参</span>
<span class="kw1">using</span> T2 <span class="sy1">=</span> X<span class="sy1">&lt;&gt;</span><span class="sy4">;</span>            <span class="co1">// 错误：第一模板形参无默认实参</span>
<span class="kw1">using</span> T3 <span class="sy1">=</span> X<span class="sy1">&lt;</span><span class="nu0">1</span><span class="sy1">&gt;</span><span class="sy4">;</span>           <span class="co1">// 错误：值 1 不匹配类型实参</span>
<span class="kw1">using</span> T4 <span class="sy1">=</span> X<span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span><span class="sy4">;</span>         <span class="co1">// 错误：第二模板形参替换失败</span>
<span class="kw1">using</span> T5 <span class="sy1">=</span> X<span class="sy1">&lt;</span>S<span class="sy1">&gt;</span><span class="sy4">;</span>           <span class="co1">// OK</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<p>如果在 <span class="t-spar">简单模板标识</span> 的模板名指名受制约的非函数模板或受制约的模板模板形参，但不是作为未知特化的成员的成员模板，而且 <span class="t-spar">简单模板标识</span> 中的所有模板实参均非待决，则必须满足受制约模板的各项关联制约：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">typename</span> T<span class="sy1">&gt;</span> concept C1 <span class="sy1">=</span> sizeof<span class="br0">(</span>T<span class="br0">)</span> <span class="sy3">!</span><span class="sy1">=</span> sizeof<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
 
<span class="kw1">template</span><span class="sy1">&lt;</span>C1 T<span class="sy1">&gt;</span> <span class="kw1">struct</span> S1 <span class="br0">{</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">template</span><span class="sy1">&lt;</span>C1 T<span class="sy1">&gt;</span> <span class="kw1">using</span> Ptr <span class="sy1">=</span> T<span class="sy2">*</span><span class="sy4">;</span>
 
S1<span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span><span class="sy2">*</span> p<span class="sy4">;</span>                         <span class="co1">// 错误：不满足制约</span>
Ptr<span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> p<span class="sy4">;</span>                         <span class="co1">// 错误：不满足制约</span>
 
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">typename</span> T<span class="sy1">&gt;</span>
<span class="kw1">struct</span> S2 <span class="br0">{</span> Ptr<span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> x<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>          <span class="co1">// 错误，不要求诊断</span>
 
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">typename</span> T<span class="sy1">&gt;</span>
<span class="kw1">struct</span> S3 <span class="br0">{</span> Ptr<span class="sy1">&lt;</span>T<span class="sy1">&gt;</span> x<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>            <span class="co1">// OK：不要求满足</span>
 
S3<span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> x<span class="sy4">;</span>                          <span class="co1">// 错误：不满足制约</span>
 
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">template</span><span class="sy1">&lt;</span>C1 T<span class="sy1">&gt;</span> <span class="kw1">class</span> X<span class="sy1">&gt;</span>
<span class="kw1">struct</span> S4 <span class="br0">{</span>
  X<span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> x<span class="sy4">;</span>                         <span class="co1">// 错误，不要求诊断</span>
<span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">typename</span> T<span class="sy1">&gt;</span> concept C2 <span class="sy1">=</span> sizeof<span class="br0">(</span>T<span class="br0">)</span> <span class="sy1">==</span> <span class="nu0">1</span><span class="sy4">;</span>
 
<span class="kw1">template</span><span class="sy1">&lt;</span>C2 T<span class="sy1">&gt;</span> <span class="kw1">struct</span> S <span class="br0">{</span> <span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw1">template</span> <span class="kw1">struct</span> S<span class="sy1">&lt;</span><span class="kw4">char</span><span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span><span class="sy1">&gt;</span><span class="sy4">;</span>         <span class="co1">// 错误：不满足制约</span>
<span class="kw1">template</span><span class="sy1">&lt;&gt;</span> <span class="kw1">struct</span> S<span class="sy1">&lt;</span><span class="kw4">char</span><span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span><span class="sy1">&gt;</span> <span class="br0">{</span> <span class="br0">}</span><span class="sy4">;</span>   <span class="co1">// 错误：不满足制约</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E6.A8.A1.E6.9D.BF.E5.8C.96.E5.AE.9E.E4.BD.93">模板化实体</span></h3>
<p>模板化实体（某些资料称之为 "temploid"）是任何定义（或对于 <span class="t-spar">lambda-表达式</span> 为创建）于模板定义内的实体。下列所有实体都是模板化实体：
</p>
<ul><li> 类/函数<span class="t-rev-inl t-since-cxx14"><span>/变量</span> <span><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></span></span>模板
</li><li> <a href="constraints.html" title="cpp/language/constraints">概念</a> <span class="t-mark-rev t-since-cxx20">(C++20 起)</span>
</li><li> 模板化实体的成员（例如类模板的非模板成员函数）
</li><li> 作为模板化实体的枚举的枚举项
</li><li> 任何模板化实体中定义或创建的实体：局部类，局部变量，友元函数，等等
</li><li> 模板化实体的声明中出现的 lambda 表达式的闭包类型
</li></ul>
<p>例如，以下模板中：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">typename</span> T<span class="sy1">&gt;</span> <span class="kw1">struct</span> A <span class="br0">{</span> <span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span> <span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<p>函数 <code>A::f</code> 不是函数模板，但仍被当做是模板化的。
</p>
<!-- 
NewPP limit report
Preprocessor visited node count: 2356/1000000
Preprocessor generated node count: 9247/1000000
Post‐expand include size: 48101/2097152 bytes
Template argument size: 13422/2097152 bytes
Highest expansion depth: 17/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:8596-0!*!0!!*!*!zh!* and timestamp 20200313102114 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/templates&amp;oldid=65288">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/templates&amp;oldid=65288</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/templates">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2020年3月13日 (星期五) 03:21。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过23,944次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.080 secs. -->
	</body>
<!-- Cached 20200313195202 -->
</html>